cmake_minimum_required(VERSION 3.26.0)
project(
  TRT_INFER
  VERSION 0.0.0.1
  DESCRIPTION "TRT Inference SDK"
  LANGUAGES CXX)

option(BUILD_TESTS "Whether to build unit testing" ON)
if(BUILD_TESTS)
  set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  include(CTest)
else()
  set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
endif(BUILD_TESTS)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS_DEBUG "/MDd")
set(CMAKE_C_FLAGS_DEBUG "/MDd")
set(CMAKE_CUDA_FLAGS_DEBUG "/MDd")
set(CUDA_NVCC_FLAGS_DEBUG "${CUDA_NVCC_FLAGS_DEBUG} -Xcompiler /MDd")

set(CMAKE_CXX_FLAGS_RELEASE "/MD")
set(CMAKE_C_FLAGS_RELEASE "/MD")
set(CMAKE_CUDA_FLAGS_RELEASE "/MD")
set(CUDA_NVCC_FLAGS_RELEASE "${CUDA_NVCC_FLAGS_RELEASE} -Xcompiler /MD")


add_definitions(-D_ITERATOR_DEBUG_LEVEL=1)

set(root_dir ${CMAKE_CURRENT_SOURCE_DIR})
set(src_dir ${root_dir}/source)
set(3rdParty_dir ${root_dir}/3rdParty)
set(install_dir ${CMAKE_BINARY_DIR})
set(CMAKE_INSTALL_PREFIX "${install_dir}/install")

set_property(GLOBAL PROPERTY USE_FOLDERS On)
ADD_DEFINITIONS(-DUNICODE)
ADD_DEFINITIONS(-D_UNICODE)

if (MSVC)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4244")
endif()

if(APPLE)
	set(LINK_TYPE STATIC)
else()
	set(LINK_TYPE SHARED)
endif(APPLE)

if((WIN32 AND NOT MINGW) OR APPLE)
  if(NOT DEFINED CMAKE_DEBUG_POSTFIX)
    set(CMAKE_DEBUG_POSTFIX "d")
  endif()
  if(NOT DEFINED CMAKE_RELEASE_POSTFIX)
    set(CMAKE_RELEASE_POSTFIX "")
  endif()
  if(NOT DEFINED CMAKE_RELWITHDEBINFO_POSTFIX)
    set(CMAKE_RELWITHDEBINFO_POSTFIX "")
  endif()
  if(NOT DEFINED CMAKE_MINSIZEREL_POSTFIX)
    set(CMAKE_MINSIZEREL_POSTFIX "")
  endif()
endif()

if(APPLE)
	set(CMAKE_LINKER_FLAGS "-Wl,rpath")
else()
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
	set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
	set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif(APPLE)


set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/RelWithDebInfo)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/RelWithDebInfo)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/RelWithDebInfo)

if(WIN32)
  include(cmake/dependencies.cmake)
else()
  include(cmake/dependencies-linux.cmake)
endif()

include(cmake/features.cmake)

option(TRT_COPY_RUNTIMES "Copy runtime libraries to the output directory" ON)
if (TRT_COPY_RUNTIMES)
  include(cmake/copy_runtimes.cmake)
endif()


add_subdirectory(source)
add_subdirectory(samples)


if(BUILD_TESTS)
  message(STATUS "Building tests ON")
  add_subdirectory(test)
endif(BUILD_TESTS)